#include<iostream>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
#include <arpa/inet.h>
#include<unistd.h>
#include <sys/epoll.h>
using namespace std;
int main()
{
  

  int sockfd=socket(AF_INET,SOCK_STREAM,0);
      if(sockfd<0)
      {
  
          cout<<"sockfd fail..."<<endl;
      }

      struct sockaddr_in server;
      server.sin_family=AF_INET;
      server.sin_addr.s_addr=inet_addr("0.0.0.0");
      server.sin_port=htons(8080);
              

      socklen_t len=sizeof(server);
      
     if( bind(sockfd,(const sockaddr*)&server,len)==0)
     {cout<<"绑定成功"<<endl;}
     else
     {cout<<"绑定失败"<<endl;}
 
      
      if(listen(sockfd,5)==0)
       {cout<<"listen 成功"<<endl;}
     struct sockaddr_in client; 
     socklen_t t = sizeof(client);

     int fd=epoll_create(5);
     int num=50;
     epoll_event EV[num]={0};
     epoll_event tt;
      int ready=0;
     struct sockaddr tp;
     socklen_t count;
     char buffer[1024];
     while(1)
     {
          ready=epoll_wait(fd,EV,num,-1);//
          if(ready==-1)
          {continue;}
  
        for(int i=0;i<num;i++)
            {
               if(EV[i].data.fd==sockfd)
               {
                
                 int newfd=accept(sockfd,&tp,&count);
                      tt.events=EPOLLIN;
                      tt.data.fd=sockfd;
                      epoll_ctl(fd,EPOLL_CTL_ADD,newfd,&tt);
               }
               else
               {
                      memset(buffer,0,sizeof(buffer)-1);
                      int n=recv(EV[i].data.fd,buffer,sizeof(buffer),0);
                      if(n>0)
                         {

                             buffer[n]=0;
                             cout<<buffer<<endl;





                         }
                      else if(n==0)
                         {

                           epoll_ctl(fd,EPOLL_CTL_DEL,EV[i].data.fd,&EV[i]);


                         }



               }



            }




     }






}